﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class User_SmsController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Users/User_Sms.cshtml";
        string mobiles = string.Empty;
        public ISms_templateService _sms_template { get; set; }
        public IUser_groupsService _user_groups { get; set; }
        public IUsersService _users { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("user_sms", CMSEnums.ActionEnum.View.ToString()); //检查权限
            mobiles = Request.Query["mobiles"];
            ViewBag.Mobiles = mobiles;
        }
        // GET: /admin/User_Sms/

        public ActionResult Index()
        {
            ShowInfo(mobiles);
            TreeBind("where is_lock=0"); //绑定类别
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ActionResult result = View(EDIT_RESULT_VIEW);
            ChkAdminLevel("user_sms", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                                                                          //检查短信内容
            if (Request.Form["txtSmsContent"].ToString().Trim() == "")
            {
                JscriptMsg("请输入短信内容！", "back");
                return result;
            }
            //检查发送类型
            if (Request.Form["rblSmsType"] == "1")
            {
                if (Request.Form["txtMobileNumbers"].ToString().Trim() == "")
                {
                    JscriptMsg("请输入手机号码！", "back");
                    return result;
                }
                //开始发送短信
                string msg = string.Empty;
                bool _result = true;//TODO _sms_message.Send(Request.Form["txtMobileNumbers"].Trim(), Request.Form["txtSmsContent"].Trim(), 2, out msg);
                if (_result)
                {
                    AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "发送手机短信"); //记录日志
                    JscriptMsg(msg, "index");
                    return result;
                }
                JscriptMsg(msg, "");
                return result;
            }
            else
            {
                ArrayList al = new ArrayList();
                var dt = _user_groups.GetList("where is_lock=0 order by grade asc,id asc");
                foreach (User_groups dr in dt)
                {
                    string v = Request.Form["cblGroupId_" + dr.id];
                    if (v.ToLower().IndexOf("true") >= 0)
                        al.Add(dr.id.ToString());
                }
                if (al.Count < 1)
                {
                    JscriptMsg("请选择会员组别！", "back");
                    return result;
                }
                string _mobiles = GetGroupMobile(al);
                //开始发送短信
                string msg = string.Empty;
                bool _result = true;//TODO new _sms_message.Send(_mobiles, Request.Form["txtSmsContent"].Trim(), 2, out msg);
                if (_result)
                {
                    AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "发送手机短信"); //记录日志
                    JscriptMsg(msg, "index");
                    return result;
                }
                JscriptMsg(msg, "back");
                return result;
            }
        }

        #region 绑定类别=================================
        private void TreeBind(string strWhere)
        {
            var dt = _user_groups.GetList(strWhere+" order by grade asc,id asc");
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (User_groups dr in dt)
            {
                dict.Add(dr.title, dr.id.ToString());
            }
            ViewData["dict"] = dict;
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(string _mobiles)
        {
            if (!string.IsNullOrEmpty(_mobiles))
            {
                ViewBag.SmsType = "1";
                ViewBag.MobileNumbers = _mobiles;
            }
            else
            {
                ViewBag.SmsType = "2";
            }
        }
        #endregion

        #region 返回会员组所有手机号码===================
        private string GetGroupMobile(ArrayList al)
        {
            StringBuilder str = new StringBuilder();
            foreach (Object obj in al)
            {
                var dt = _users.GetList("where group_id=" + Convert.ToInt32(obj)+ " order by reg_time desc,id desc");
                foreach (NetCoreCMS.Models.Users dr in dt)
                {
                    if (!string.IsNullOrEmpty(dr.mobile))
                    {
                        str.Append(dr.mobile + ",");
                    }
                }
            }
            return Utils.DelLastComma(str.ToString());
        }
        #endregion
    }
}
